package com.google.code.netbuilder.dal.providersimpl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.google.code.netbuilder.dal.entities.Port;
import com.google.code.netbuilder.dal.entities.Wire;
import com.google.code.netbuilder.dal.providers.IWireProvider;
import com.google.code.netbuilder.dal.services.JDBCServise;

public class WireProviderJDBC implements IWireProvider {

	@Override
	public void save(Wire entity) throws RuntimeException{
		String query = "INSERT INTO wire(id, from_port_id, to_port_id, number, date)" +
				" VALUES (?,?,?,?,?);";
		Connection con = null;
		PreparedStatement pst = null;
		try
		{
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			int rs=-1;
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getId());
			pst.setInt(2, entity.getFromPortId());
			pst.setInt(3, entity.getToPortId());
			pst.setInt(4, entity.getNumber());
			pst.setDate(5, new Date(entity.getDate().getTime()));
			pst.executeUpdate();
		}
		catch(SQLException e)
		{
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}

	}

	@Override
	public void update(Wire entity) throws RuntimeException{
		String query = "UPDATE wire SET from_port_id=?, to_port_id=?, number=?, date=? WHERE id=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getFromPortId());
			pst.setInt(2, entity.getToPortId());
			pst.setInt(3, entity.getNumber());
			pst.setDate(4, new Date(entity.getDate().getTime()));
			pst.setInt(5, entity.getId());
			pst.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public void delete(Wire entity) throws RuntimeException{
		String query = "DELETE FROM wire WHERE id=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getId());		
			pst.executeUpdate();	
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}		
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public List findAll() throws RuntimeException{
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Wire> wires = null;
		
		try {
			con = JDBCServise.getConnection();
        
			pst = con.prepareStatement("Select * From wire");
			rs = pst.executeQuery();
        
			wires = new ArrayList<Wire>();
        
			while (rs.next()) {
				Wire wire = new Wire();
				wire.setId(rs.getInt("id"));
				wire.setFromPortId(rs.getInt("from_port_id"));
				wire.setToPortId(rs.getInt("to_port_id"));
				wire.setNumber(rs.getInt("number"));
				wire.setDate(rs.getDate("date").getTime());
			}
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}	
		
        return wires;		
	}

	@Override
	public Wire findByID(Integer id) throws RuntimeException{
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		Wire wire = null;
		try {
			con = JDBCServise.getConnection();
	     
			if (con == null) throw new SQLException("No Data Base Connection");          
			pst = con.prepareStatement("SELECT * FROM wire where id="
        								+ id.toString());
			rs = pst.executeQuery();
       
			if (!rs.first()) throw new SQLException("Wire not found");
			
			wire = new Wire();
        
			wire.setId(rs.getInt("id"));
			wire.setFromPortId(rs.getInt("from_port_id"));
			wire.setToPortId(rs.getInt("to_port_id"));
			wire.setNumber(rs.getInt("number"));
			wire.setDate(rs.getDate("date").getTime());
			
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}
		return wire;
	}

}
